//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Money.h"
using namespace Cephei::QL;
#include <gen/QL/Currency.h>
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::CMoney::CMoney (Double value, Cephei::QL::ICurrency^ currency) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CCurrency^ _Ccurrency;
    try
    {
#ifdef HANDLE
        _phMoney = NULL;
#endif
        QuantLib::Decimal _value = (QuantLib::Decimal)ValueHelper::Convert (value); //d
        _Ccurrency = safe_cast<CCurrency^> (currency);
        _Ccurrency->Lock();
        QuantLib::Currency& _currency = static_cast<QuantLib::Currency&> (_Ccurrency->GetReference ()); 
        _ppMoney = new boost::shared_ptr<QuantLib::Money> (new QuantLib::Money ( _value,  _currency ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccurrency != nullptr) _Ccurrency->Unlock();
    }
}
Cephei::QL::CMoney::CMoney (Cephei::QL::ICurrency^ currency, Double value) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CCurrency^ _Ccurrency;
    try
    {
#ifdef HANDLE
        _phMoney = NULL;
#endif
        _Ccurrency = safe_cast<CCurrency^> (currency);
        _Ccurrency->Lock();
        QuantLib::Currency& _currency = static_cast<QuantLib::Currency&> (_Ccurrency->GetReference ()); 
        QuantLib::Decimal _value = (QuantLib::Decimal)ValueHelper::Convert (value); //d
        _ppMoney = new boost::shared_ptr<QuantLib::Money> (new QuantLib::Money ( _currency,  _value ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccurrency != nullptr) _Ccurrency->Unlock();
    }
}
Cephei::QL::CMoney::CMoney () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phMoney = NULL;
#endif
        _ppMoney = new boost::shared_ptr<QuantLib::Money> (new QuantLib::Money ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::CMoney::CMoney (boost::shared_ptr<QuantLib::Money>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMoney = NULL;
#endif
	_ppMoney = &childNative;
    
}
Cephei::QL::CMoney::CMoney (QuantLib::Money& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phMoney = NULL;
#endif
	_ppMoney = new boost::shared_ptr<QuantLib::Money> (&childNative);
    
    _MoneyOwner = owner;
    
}

Cephei::QL::CMoney::CMoney (CMoney^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMoney = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMoney = new boost::shared_ptr<QuantLib::Money> (copy->GetShared());
        
    }
}
Cephei::QL::CMoney::CMoney (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMoney = NULL;
#endif
	if (!t->IsSubclassOf(CMoney::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::CMoney::CMoney (QuantLib::Handle<QuantLib::Money>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMoney = &childNative;
	_ppMoney = &static_cast<boost::shared_ptr<QuantLib::Money>>(childNative.currentLink());
    
    _MoneyOwner = owner;
}
Cephei::QL::CMoney::CMoney (QuantLib::Handle<QuantLib::Money> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMoney = &childNative;
	_ppMoney = &static_cast<boost::shared_ptr<QuantLib::Money>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::CMoney::CMoney (QuantLib::Money childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMoney = NULL;
#endif
	_ppMoney = new boost::shared_ptr<QuantLib::Money> (new QuantLib::Money (childNative));
    
}
#endif

Cephei::QL::CMoney::~CMoney ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppMoney != NULL)
    {
	    delete _ppMoney;
        _ppMoney = NULL;
    }
}
Cephei::QL::CMoney::!CMoney ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppMoney != NULL)
    {
	    delete _ppMoney;
    }
}
QuantLib::Money& Cephei::QL::CMoney::GetReference ()
{
    if (_ppMoney == NULL) throw REFNEW NativeNullException ();
	return **_ppMoney;
}
boost::shared_ptr<QuantLib::Money>& Cephei::QL::CMoney::GetShared ()
{
    if (_ppMoney == NULL) throw REFNEW NativeNullException ();
	return *_ppMoney;
}
QuantLib::Money* Cephei::QL::CMoney::GetPointer ()
{
    if (_ppMoney == NULL) throw REFNEW NativeNullException ();
	return &**_ppMoney;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Money>& Cephei::QL::CMoney::GetHandle ()
{
	if (_phMoney == NULL)
	{
		_phMoney = new Handle<QuantLib::Money> (*_ppMoney);
	}
	return *_phMoney;
}
#endif
bool Cephei::QL::CMoney::HasNative () 
{
	return (_ppMoney != NULL);
}

Cephei::QL::ICurrency^ Cephei::QL::CMoney::Currency::get ()
{
    try
    {
    	QuantLib::Currency& _rv = (QuantLib::Currency&)(*_ppMoney)->currency ( );   
        Cephei::QL::CCurrency^ _nrv = REFNEW Cephei::QL::CCurrency (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IMoney^ Cephei::QL::CMoney::Rounded::get ()
{
    try
    {
    	QuantLib::Money _rv = (QuantLib::Money)(*_ppMoney)->rounded ( );   
        Cephei::QL::CMoney^ _nrv = REFNEW Cephei::QL::CMoney (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::CMoney::Value::get ()
{
    try
    {
    	QuantLib::Decimal _rv = (QuantLib::Decimal)(*_ppMoney)->value ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::IMoney^ Cephei::QL::CMoney_Factory::Create (Double value, Cephei::QL::ICurrency^ currency)
{
    return REFNEW CMoney ( value,  currency);
}
Cephei::QL::IMoney^ Cephei::QL::CMoney_Factory::Create (Cephei::QL::ICurrency^ currency, Double value)
{
    return REFNEW CMoney ( currency,  value);
}
Cephei::QL::IMoney^ Cephei::QL::CMoney_Factory::Create ()
{
    return REFNEW CMoney ();
}
